﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenSSL.X509;
using OpenSSL.Crypto;
using Sicurezza.Entita;
using OpenSSL.Core;

namespace Sicurezza.Client.Manager
{
    public class RenewManager
    {
        //public static void SendRequest(int p)
        //{
        //    throw new NotImplementedException();
        //}

        //public static void SendRequest(OpenSSL.X509.X509Certificate Certificato)
        //{
        //    throw new NotImplementedException();
        //}

        //public static void SendRequest(X509Certificate Certificato, string password)
        //{
        //    string serial = Certificato.SerialNumber.ToString();
        //    byte[] signature = SSLManager.Sign(serial, Certificato, password);
        //    WSManager.Renew(serial, signature);
        //}

        //public static void SendRequest(X509Certificate Certificato, OpenSSL.Crypto.RSA key)
        //{
        //    string serial = Certificato.SerialNumber.ToString();
        //    byte[] signature = SSLManager.Sign(serial, key);
        //    WSManager.Renew(serial, signature);
        //}

        public static string SendRequest(X509Certificate Certificato, CryptoKey key, RSA newkey)
        {
            string serial = Certificato.SerialNumber.ToString();
            string newkeyPEM = newkey.PublicKeyAsPEM;
            byte[] signature = SSLManager.Sign(serial+newkeyPEM, key);
            string RenId = WSManager.Renew(serial, newkeyPEM, signature);
            return RenId;

        }

        public static void Save(string RenId, int serial, RSA newkey, string password)
        {
            FileManager.SaveRequest(RenId, serial, newkey, password);
        }

        public static string Check(string ReqId)
        {
            string cert = WSManager.GetCertificate(ReqId);
            if (!string.IsNullOrEmpty(cert))
            {
                X509Certificate c;
                try
                {
                    c = new X509Certificate(new BIO(cert));


                    int serial = c.SerialNumber;
                    FileManager.MovePrivateKey(ReqId, serial);
                    FileManager.DeleteRenew(ReqId);
                    X509Manager.Save(c);
                }
                catch (OpenSslException ex)
                {
                    FileManager.RejectRequest(ReqId);
                    throw new ManagerException(cert);
                }

            }
            return cert;
        }

        public static List<Pair> GetAll()
        {
            string[] paths = FileManager.GetRenews();
            List<Pair> elements = new List<Pair>();
            Pair p = null;
            X509Certificate c = null;
            for (int i = 0; i < paths.Length; i++)
            {
                p = new Pair();
                p.key = paths[i];
                c = new X509Certificate(new BIO(FileManager.LoadCertificate(paths[i])));
                p.value = c.Subject.ToString();
                elements.Add(p);
            }

            return elements;
        }

        public static X509Certificate Get(string ReqId)
        {
            string cert = FileManager.LoadRenew(ReqId);
            return new X509Certificate(new BIO(cert));

        }
    }
}
